﻿#include "inherentlnk.h"
#include <QDebug>
#include <QFile>
#include <QMouseEvent>
#include <QToolTip>
#include <QMimeData>
#include <widget.h>
#include <QPainter>
#include "settinghandler.h"

InherentLNK::InherentLNK(const QString &svg_name, const QString &lnk_name, QWidget *parent, bool move_able)
 : QPushButton(parent), name(lnk_name), moveable(move_able), have_rubbish(false)
{
    qDebug() << "InherentLNK 创建了";

    setFlat(true);
    setIcon(QIcon(svg_name));
    set_size(SettingHandler::get_instance()->get_lnk_size(), SettingHandler::get_instance()->get_lnk_size());

    if (lnk_name == "回收站")
        setAcceptDrops(true);  // 接收鼠标拖动事件
}

void InherentLNK::set_size(int wd, int ht)
{
    setIconSize(QSize(wd, ht));
    resize(QSize(wd, ht));
}

// 拖进来了
void InherentLNK::dragEnterEvent(QDragEnterEvent *event)
{
    change_icon(have_rubbish?":/svgs/rubbish_open_full.svg":":/svgs/rubbish_open.svg");
    event->acceptProposedAction();    //接收拖拽信号
}

// 拖进来松手了
void InherentLNK::dropEvent(QDropEvent *event)
{
    have_rubbish = true;
    change_icon(have_rubbish?":/svgs/rubbish_close_full.svg":":/svgs/rubbish_close.svg");
    // 如果拖到了回收站上面
    for (const auto &url : event->mimeData()->urls())
    {
        QFile::moveToTrash(url.toLocalFile());
    }
}

// 拖出去了
void InherentLNK::dragLeaveEvent(QDragLeaveEvent *)
{
    change_icon(have_rubbish?":/svgs/rubbish_close_full.svg":":/svgs/rubbish_close.svg");
}

void InherentLNK::mousePressEvent(QMouseEvent *event)
{
    // 变大
    set_size(SettingHandler::get_instance()->get_widget_height(), SettingHandler::get_instance()->get_widget_height());
    move(old_x - SettingHandler::get_instance()->get_border_size(), 0);

    this_is_clicked = true;
    if (moveable)
    {
        auto pos = event->pos();
        press_x = pos.x();
        press_y = pos.y();
    }
}

void InherentLNK::mouseReleaseEvent(QMouseEvent *event)
{
    // 还原
    set_size(SettingHandler::get_instance()->get_lnk_size(), SettingHandler::get_instance()->get_lnk_size());
    move(old_x, old_y);

    if (this_is_clicked)
    {
        auto what_button = event->button();
        if (what_button == Qt::LeftButton)
            emit left_clicked();
        if (what_button == Qt::RightButton)
            emit right_clicked();
    }
    else if (moveable)
    {
        auto window_pos = event->scenePosition();

        if (window_pos.y() < 0)
            emit drag_out();
        else
            emit not_drag_out();
    }
}

void InherentLNK::mouseMoveEvent(QMouseEvent *event)
{
    this_is_clicked = false;
    if (moveable)
    {
        auto window_pos = event->scenePosition();
        move(window_pos.x() - press_x, window_pos.y() - press_y);
    }
}

void InherentLNK::enterEvent(QEnterEvent *)
{
    old_x = x();
    old_y = y();
    QToolTip::showText(mapToGlobal(QPoint(0, -50)), name, this);
    // 变大
    set_size(SettingHandler::get_instance()->get_widget_height(), SettingHandler::get_instance()->get_widget_height());
    move(old_x - SettingHandler::get_instance()->get_border_size(), 0);
}

void InherentLNK::leaveEvent(QEvent *)
{
    // 还原
    set_size(SettingHandler::get_instance()->get_lnk_size(), SettingHandler::get_instance()->get_lnk_size());
    move(old_x, old_y);
}

InherentLNK::~InherentLNK()
{
    qDebug() << "InherentLNK 销毁了";
}
